#include <stdio.h>
#include <stdlib.h>
#include "fun.h"
#include "ziku.h"
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <linux/input.h>
#include <unistd.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <stdbool.h>


//显示图片
//参数1：图片的路径 
//参数2：映射关系 
void show_bmp(const char *path,int *memp)
{
	char buf[3] = {};
	int offset = 0,width = 0,height = 0;
	short bpp = 0;

	//打开图片文件 
	int fdp = open(path,O_RDWR);
	if(fdp == -1){
		perror("open");
		return;
	} 
	
	//读文件类型
	read(fdp,buf,2);
	//读图像数据里文件开头的距离
	lseek(fdp,10,SEEK_SET);
	read(fdp,&offset,4);
	//读图片宽度和高度
	lseek(fdp,4,SEEK_CUR);
	read(fdp,&width,4);
	read(fdp,&height,4);
	//读像素位数
	lseek(fdp,2,SEEK_CUR);
	read(fdp,&bpp,2);
	
	//打印图片参数 
	printf("buf = %s\n",buf); 
	printf("offset = %d\n",offset);
	printf("width = %d,height = %d\n",width,height);
	printf("bpp = %hd\n",bpp);
	
	//读取RGB数据
	lseek(fdp,offset,SEEK_SET);
	read(fdp,picture,width*height*bpp/8);

	
	//显示
	int i = 0,j = 0;
	//将图片显示到LCD上 
	for(i = 0;i < 480;i ++){
		for(j = 0;j < 800;j ++){
			//*(memp+i*800+j) = RED;
			*(memp + i * 800 + j) = picture[3 * ((479-i) * 800 + j)]|picture[3 * ((479-i) * 800+j) + 1]<<8|picture[3 * ((479-i) * 800+j) + 2]<<16;   //R
		}
		
	} 
	
	//关闭图片文件		
	close(fdp); 
}



//登录界面
//参数1：映射关系 参数2：触摸屏  参数3 ：点击事件 
void login_image(int *memp,int fdc,struct input_event evt)
{
	//显示登录界面 
	show_bmp("/mnt/udisk/register.BMP",memp);	

	char key[] = "******";  //用户输入保存地 
	
	int xc = 0,yc = 0;  //触摸坐标
	 
	int i = 0,j = 0;  //j用来追溯输入的密码位置下标 
	char passwd[] = "123456";  //密码初始值 

end:	
	
	//将密码所占的像素位置置空 
	for(i = 0;i < 6;i++){
		lcd_put_ascii(320 + i * 10,40,' '); 
	}
	
	//用户输入密码阶段 
	while(read(fdc,&evt,sizeof(evt))>0){
		bool flag = true;  //用来判断用户是否删除输入过程中的密码 
		
		if(evt.type == EV_ABS){
           	if(evt.code == ABS_X)
               	xc = evt.value;
           	else if(evt.code == ABS_Y)
               	yc = evt.value;
       	}
       	//按下时产生操作
        else if(evt.type==EV_KEY&&evt.value==1){
           	printf("(%d,%d)\n",xc,yc);

		if(xc>50&&xc<200&&yc>210&&yc<320){
			return ;
		}
           	
           	//游客登录 
           	if(xc>485&&xc<694&&yc>440&&yc<520){
           		//最后一个参数为-1表示，游客登录模式下 不能访问文件系统 
				main_image(memp,fdc,evt,-1);
				login_image(memp,fdc,evt);
			}
				
			if(xc>443&&xc<518&&yc>385&&yc<451){
				//0
				//在相应位置显示0 
				lcd_put_ascii(320+j*10,40,'0'); 
				//往密码数组对应位置中存入0 
				key[j] = '0';
				
				//等待一秒，将输入的数字密码化 
				sleep(1);
				lcd_put_ascii(320+j*10,40,'*'); 
				
				//输入一个有效数字后，将布尔值置为假 
				flag = false;
			}
				
			if(yc>97&&yc<188){
				//1
				if(xc>320&&xc<420){
					//printf("1");
					lcd_put_ascii(320+j*10,40,'1'); 
					key[j] = '1';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
				//2
				if(xc>450&&xc<520){
					//printf("2"); 
					lcd_put_ascii(320+j*10,40,'2'); 
					key[j] = '2';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
				//3
				if(xc>550&&xc<620){
					//printf("3"); 
					lcd_put_ascii(320+j*10,40,'3'); 
					key[j] = '3';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
			}
            	
            if(yc>200&&yc<275){
            	//4
				if(xc>320&&xc<420){
					//printf("4");
					lcd_put_ascii(320+j*10,40,'4'); 
					key[j] = '4';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
				//5
				if(xc>450&&xc<520){
					//printf("5"); 
					lcd_put_ascii(320+j*10,40,'5'); 
					key[j] = '5';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
				//6
				if(xc>550&&xc<620){
					//printf("6"); 
					lcd_put_ascii(320+j*10,40,'6'); 
					key[j] = '6';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
			}
				
			if(yc>280&&yc<350){
				//7
				if(xc>320&&xc<420){
					//printf("7");
					lcd_put_ascii(320+j*10,40,'7'); 
					key[j] = '7';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
				//8
				if(xc>450&&xc<520){
					//printf("8"); 
					lcd_put_ascii(320+j*10,40,'8'); 
					key[j] = '8';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
				//9
				if(xc>550&&xc<620){
					//printf("9"); 
					lcd_put_ascii(320+j*10,40,'9'); 
					key[j] = '9';
					sleep(1);
					lcd_put_ascii(320+j*10,40,'*'); 
					flag = false;
				}
			}
				
			//输错密码，删除功能 
			if(xc>250&&xc<340&&yc>460&&yc<520){
				//将当前的位置减一之后的位置置空，并且j减一，即下标减一，之后重新输入 
				lcd_put_ascii(320+(j-1)*10,40,' '); 
				j--;
			}
				
			//只有输入数字（即触摸屏位置）为有效值，才继续输入下一个 
			if(flag==false)
				j++;
				
			//判断密码是否输入正确 
			if(j==6){
				//密码正确跳到主界面 
				if(strcmp(passwd,key) == 0)
					{
						//最后一个参数为1，表示可以访问文件系统 
						main_image(memp,fdc,evt,1);
						login_image(memp,fdc,evt);
					}
				else{
					//密码错误重新输入 
					lcd_put_ascii(320,40,'e');
					lcd_put_ascii(320+1*10,40,'r');
					lcd_put_ascii(320+2*10,40,'r');
					lcd_put_ascii(320+3*10,40,'o');
					lcd_put_ascii(320+4*10,40,'r');
					lcd_put_ascii(320+5*10,40,'!');
					sleep(2);
					 
					j=0; //将输入的所有值清空，重新输入 
					goto end;
				}
				//printf("%s\n",key);
			}
       	}
		
   	}
}

//主界面 
//参数1：映射关系   参数2：触摸屏  参数3 ： 触摸事件  参数4 ：判断是否可以访问文件系统 
void main_image(int *memp,int fdc,struct input_event evt,int m)
{
	//显示主界面 
	show_bmp("/mnt/udisk/main.BMP",memp);
	
	int xc = 0,yc = 0;  //时刻记录触摸坐标 
	
	while(read(fdc,&evt,sizeof(evt))>0){
		if(evt.type==EV_ABS){
           	if(evt.code==ABS_X)
       			xc = evt.value;
       		else if(evt.code==ABS_Y)
       			yc = evt.value;
       	}
     	//按下时产生操作
       	else if(evt.type==EV_KEY&&evt.value==1){
			printf("(%d,%d)\n",xc,yc);
		
			//返回功能，显示注册界面 
      		if(xc>880&&xc<1000&&yc>531&&yc<600){
				return;
			}
				
			//游戏操作
			if(xc>450&&xc<600&&yc>140&&yc<270){
				return board_ball(memp,fdc,evt);
			}
				
			//文件操作 
			if(xc>450&&xc<600&&yc>350&&yc<520){
				//用户登录模式下 
				if(m == 1){
					file_image(memp,fdc);
					show_bmp("/mnt/udisk/main.BMP",memp);
				}
				//游客登录模式下 
				else{
					lcd_put_asciiR(350,920,'N');
					lcd_put_asciiR(360,920,'o');
					lcd_put_asciiR(370,920,' ');
					lcd_put_asciiR(380,920,'p');
					lcd_put_asciiR(390,920,'e');
					lcd_put_asciiR(400,920,'r');
					lcd_put_asciiR(410,920,'m');
					lcd_put_asciiR(420,920,'i');
					lcd_put_asciiR(430,920,'s');
					lcd_put_asciiR(440,920,'s');
					lcd_put_asciiR(450,920,'i');
					lcd_put_asciiR(460,920,'o');
					lcd_put_asciiR(470,920,'n');
					lcd_put_asciiR(480,920,'!');
					lcd_put_asciiR(490,920,'!');
					lcd_put_asciiR(500,920,'!');
				}
			} 
					 
			//图片操作 
			if(xc>720&&xc<870&&yc>130&&yc<280){
				image_image(memp,fdc);
				show_bmp("/mnt/udisk/main.BMP",memp);
			}
				
			//锁屏操作 
			if(xc>720&&xc<870&&yc>350&&yc<520){
				return;
			}	
				
			//音乐操作 
			if(xc>190&&xc<360&&yc>350&&yc<520){
				music_image(memp,fdc);
				show_bmp("/mnt/udisk/main.BMP",memp);
			}
				
			//视频操作 
			if(xc>190&&xc<360&&yc>130&&yc<280){
				video_image(memp,fdc);
				show_bmp("/mnt/udisk/main.BMP",memp);
			}
		}
	}
}


//视频操作
//参数1 ：映射关系，参数2 ：触摸屏 
void video_image(int *memp,int fdc)
{
	//显示视频初始图片 
	show_bmp("/mnt/udisk/video_image.BMP",memp);
	
	int xc = 0,yc = 0; //触摸坐标 
	
	char buf[256] = {};  //用于拼接文件路径 
	
	int i = 0;  //用于寻找链表位置 
	
	bool flag = false; //用于暂停继续操作 

	struct dirent *dp = NULL;

	//创建成功 
	if(headv->next == headv->prev && headv->next == headv){
		
		//打开视频目录 
		DIR *dir = opendir("/mnt/udisk/Video");
		if(!dir){
			perror("opendir");
			return;
		}
		
		while(dp = readdir(dir)){
			
			////跳过文件名为  .  和  ..  的文件 
			if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
				continue;
			else {
				//将文件名插入到视频链表中 
				insert(headv->prev, dp->d_name);
				
			}
		}
		
		
		
		//关闭Video目录 
		closedir(dir);
	}
	travelBB(headv,memp);  //将此时视频文件显示到对应位置上 
	
	
	while(read(fdc,&evt,sizeof(evt))>0){
		if(evt.type==EV_ABS){
           	if(evt.code==ABS_X)
           		xc = evt.value;
       		else if(evt.code==ABS_Y)
       			yc = evt.value;
       	}
		//按下时产生操作
       	else if(evt.type==EV_KEY&&evt.value==1){
			printf("(%d,%d)\n",xc,yc);
			
			//返回 
			if(xc>780&&xc<970&&yc>470&&yc<570){
				system("killall -KILL mplayer &");
				return ;
			}
			
			//上一个 
			if(xc>780&&xc<970&&yc>60&&yc<100){
				//位置减一 
				i--;
				
				//寻找文件 
				dlist_t p = find_by_index(headv, i);


				lcd_put_asciiR(100,280,'P');
				lcd_put_asciiR(110,280,'l');
				lcd_put_asciiR(120,280,'a');
				lcd_put_asciiR(130,280,'y');
				lcd_put_asciiR(140,280,'i');
				lcd_put_asciiR(150,280,'n');
				lcd_put_asciiR(160,280,'g');
				lcd_put_asciiR(170,280,'.');
				lcd_put_asciiR(180,280,'.');
				lcd_put_asciiR(190,280,'.');
			
				int j = 0;
				while(j<20){
					lcd_put_asciiB(100+j*10,300,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(100+j*10,300,p->name[j]);
					j++;
				}

				
				//打开视频文件 
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
		 
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				
				//停止前一个 视频文件 
				system("killall -KILL mplayer &");
				
				sleep(1);
				
				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 380 -y 240 /mnt/udisk/Video/%s &",p->name);
				
				//播放当前视频文件 
				system(buf_p);

				flag = true;  //正在播放 flag 置为真 
			}
			
			//播放下一个 
			if(xc>780&&xc<970&&yc>140&&yc<210){
				//位置加一 
				i++;
				
				//寻找文件 
				dlist_t p = find_by_index(headv, i);


				lcd_put_asciiR(100,280,'P');
				lcd_put_asciiR(110,280,'l');
				lcd_put_asciiR(120,280,'a');
				lcd_put_asciiR(130,280,'y');
				lcd_put_asciiR(140,280,'i');
				lcd_put_asciiR(150,280,'n');
				lcd_put_asciiR(160,280,'g');
				lcd_put_asciiR(170,280,'.');
				lcd_put_asciiR(180,280,'.');
				lcd_put_asciiR(190,280,'.');
			
				int j = 0;
				while(j<20){
					lcd_put_asciiB(100+j*10,300,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(100+j*10,300,p->name[j]);
					j++;
				}

				
				//打开视频文件 
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);	
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				
				//停止上一个视频 
				system("killall -KILL mplayer &");
				sleep(1);
				
				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 380 -y 240 /mnt/udisk/Video/%s &",p->name);
				
				//播放当前视频 
				system(buf_p);

				flag = true;
			}
			
			//暂停继续操作 
			if(xc>780&&xc<970&&yc>250&&yc<300){
				if(flag==true){
					printf("\n暂停播放\n");
					system("killall -STOP mplayer &");
					flag = false;
					
				}else {
					printf("\n继续播放\n");
					system("killall -CONT mplayer &");
					flag = true;
				}
			}
			
			//二倍速
			if(xc>530&&xc<730&&yc>470&&yc<570){
				printf("二倍速 \n");
				system("killall -KILL mplayer &");
				sleep(1);
				dlist_t p = find_by_index(headv, i);
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 400 -y 240 -speed 2.0 /mnt/udisk/Video/%s &",p->name);
				system(buf_p);

				flag = false;
			}

			//快进快退操作 待定 
			if(xc>780&&xc<970&&yc>340&&yc<400){

			}
			
			//增大减小音量 
			if(xc>530&&xc<730&&yc>360&&yc<450){
				if(flag == true){
					printf("减小音量  \n");
					system("killall -KILL mplayer &");
					sleep(1);
					dlist_t p = find_by_index(headv, i);
					sprintf(buf,"/mnt/udisk/Video/%s",p->name);
					int fd = open(buf,O_RDONLY);
					if(fd == -1){
						perror("open1");
						return ;
					}
					char buf_p[256]={};
					sprintf(buf_p,"mplayer -af volume=-10 -softvol -softvol-max 200 -zoom -x 400 -y 240 /mnt/udisk/Video/%s &",p->name);
					system(buf_p);

					flag = false;
				}
				else {
					printf("增大音量  \n");
					system("killall -KILL mplayer &");
					sleep(1);
					dlist_t p = find_by_index(headv, i);
					sprintf(buf,"/mnt/udisk/Video/%s",p->name);
					int fd = open(buf,O_RDONLY);
					if(fd == -1){
						perror("open1");
						return ;
					}
					char buf_p[256]={};
					sprintf(buf_p,"mplayer -af volume=20 -softvol -softvol-max 200 -zoom -x 400 -y 240 /mnt/udisk/Video/%s &",p->name);
					system(buf_p);

					flag = true;


				}
			}

			
			//全屏操作 
			if(xc>20&&xc<200&&yc>470&&yc<570){
				if(flag == true){
					system("killall -KILL mplayer &");
					sleep(1);
					dlist_t p = find_by_index(headv, i);
					sprintf(buf,"/mnt/udisk/Video/%s",p->name);
					int fd = open(buf,O_RDONLY);
					if(fd == -1){
						perror("open1");
						return ;
					}
					char buf_p[256]={};
					sprintf(buf_p,"mplayer -zoom -x 800 -y 480 /mnt/udisk/Video/%s &",p->name);
					system(buf_p);

					flag = false;

				}
				else {
					system("killall -KILL mplayer &");
					sleep(1);
					dlist_t p = find_by_index(headv, i);
					sprintf(buf,"/mnt/udisk/Video/%s",p->name);
					int fd = open(buf,O_RDONLY);
					if(fd == -1){
						perror("open1");
						return ;
					}
					show_bmp("/mnt/udisk/video_image.BMP",memp);

					travelBB(headv,memp);  //将此时视频文件显示到对应位置上 

					lcd_put_asciiR(100,280,'P');
					lcd_put_asciiR(110,280,'l');
					lcd_put_asciiR(120,280,'a');
					lcd_put_asciiR(130,280,'y');
					lcd_put_asciiR(140,280,'i');
					lcd_put_asciiR(150,280,'n');
					lcd_put_asciiR(160,280,'g');
					lcd_put_asciiR(170,280,'.');
					lcd_put_asciiR(180,280,'.');
					lcd_put_asciiR(190,280,'.');
			
					int j = 0;
					while(j<20){
						lcd_put_asciiB(100+j*10,300,' ');
						j++;
					}				
					sleep(1);
					j=0;
					while(p->name[j]!='\0'){
						lcd_put_asciiR(100+j*10,300,p->name[j]);
						j++;
					}
					

					char buf_p[256]={};
					sprintf(buf_p,"mplayer -zoom -x 380 -y 240 /mnt/udisk/Video/%s &",p->name);
					system(buf_p);

					flag = true;
					
				}

			}			
			
			//点击文件，播放指定视频 
			if(xc>550&&xc<730&&yc>130&&yc<190){
				dlist_t p = find_by_index(headv, 1);
				
				lcd_put_asciiR(100,280,'P');
				lcd_put_asciiR(110,280,'l');
				lcd_put_asciiR(120,280,'a');
				lcd_put_asciiR(130,280,'y');
				lcd_put_asciiR(140,280,'i');
				lcd_put_asciiR(150,280,'n');
				lcd_put_asciiR(160,280,'g');
				lcd_put_asciiR(170,280,'.');
				lcd_put_asciiR(180,280,'.');
				lcd_put_asciiR(190,280,'.');
			
				int j = 0;
				while(j<20){
					lcd_put_asciiB(100+j*10,300,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(100+j*10,300,p->name[j]);
					j++;
				}


				
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				system("killall -KILL mplayer &");
				sleep(1);
				
				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 380 -y 240 /mnt/udisk/Video/%s &",p->name);
				system(buf_p);
				i = 1;
				flag = true;

			}
			//点击文件，播放指定视频 
			if(xc>550&&xc<730&&yc>200&&yc<270){
				dlist_t p = find_by_index(headv, 2);

				lcd_put_asciiR(100,280,'P');
				lcd_put_asciiR(110,280,'l');
				lcd_put_asciiR(120,280,'a');
				lcd_put_asciiR(130,280,'y');
				lcd_put_asciiR(140,280,'i');
				lcd_put_asciiR(150,280,'n');
				lcd_put_asciiR(160,280,'g');
				lcd_put_asciiR(170,280,'.');
				lcd_put_asciiR(180,280,'.');
				lcd_put_asciiR(190,280,'.');
			
				int j = 0;
				while(j<20){
					lcd_put_asciiB(100+j*10,300,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(100+j*10,300,p->name[j]);
					j++;
				}




				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				system("killall -KILL mplayer &");
				sleep(1);

				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 380 -y 240 /mnt/udisk/Video/%s &",p->name);
				system(buf_p);
				i = 2;
				flag = true;
			}
			//点击文件，播放指定视频 
			if(xc>550&&xc<730&&yc>270&&yc<330){
				dlist_t p = find_by_index(headv, 3);

				lcd_put_asciiR(100,280,'P');
				lcd_put_asciiR(110,280,'l');
				lcd_put_asciiR(120,280,'a');
				lcd_put_asciiR(130,280,'y');
				lcd_put_asciiR(140,280,'i');
				lcd_put_asciiR(150,280,'n');
				lcd_put_asciiR(160,280,'g');
				lcd_put_asciiR(170,280,'.');
				lcd_put_asciiR(180,280,'.');
				lcd_put_asciiR(190,280,'.');
			
				int j = 0;
				while(j<20){
					lcd_put_asciiB(100+j*10,300,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(100+j*10,300,p->name[j]);
					j++;
				}

			
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				system("killall -KILL mplayer &");
				sleep(1);

				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 380 -y 240 /mnt/udisk/Video/%s &",p->name);
				system(buf_p);
				i = 3;
				flag = true;
			}
			if(xc>290&&xc<420&&yc>470&&570){
				printf("关闭文件\n");
				system("killall -KILL mplayer &");
				sleep(1);
				show_bmp("/mnt/udisk/video_image.BMP",memp);

				travelBB(headv,memp);  //将此时视频文件显示到对应位置上 
			}


		}
	}
}

//音乐操作 
//参数1 ：映射关系 参数2：触摸屏 
void music_image(int *memp,int fdc)
{
	//显示音乐初始化图片 
	show_bmp("/mnt/udisk/music_image.BMP",memp);
	
	int xc = 0,yc = 0;  //触摸位置
		 
	char buf[256] = {};//用于拼接文件路径
	 
	int i = 0;//用于寻找链表位置 
	
	bool flag = false;//用于暂停继续操作

	struct dirent *dp = NULL;
	if(headm->next == headm->prev && headm->next == headm){
		
		//打开音乐目录 
		DIR *dir = opendir("/mnt/udisk/Music");
		if(!dir){
			perror("opendir");
			return;
		}
		
		while(dp = readdir(dir)){
			
			//跳过文件名为  .  和  ..  的文件 
			if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
				continue;
			else {
				//将文件名插入到音乐链表中 
				insert(headm->prev, dp->d_name);	
			}
		}
		
		
		//关闭Music目录 
		closedir(dir);
	}

	travelRR(headm,memp);  //将此时的音乐文件显示到指定位置上 

	while(read(fdc,&evt,sizeof(evt))>0){
		if(evt.type==EV_ABS){
           	if(evt.code==ABS_X)
           		xc = evt.value;
       		else if(evt.code==ABS_Y)
           		yc = evt.value;
       	}
		//按下时产生操作
       	else if(evt.type==EV_KEY&&evt.value==1){
			printf("(%d,%d)\n",xc,yc);
			
			if(xc>580&&xc<680&&yc>460&&yc<560){
				printf("关闭文件\n");
				system("killall -KILL madplay &");
			}		
			//返回操作 
			if(xc>830&&xc<940&&yc>460&&yc<560){
				system("killall -KILL madplay &");
				return ;
			}
			//点击文件，播放指定文件 
			if(xc>80&&xc<280&&yc>200&&yc<270){
				
				i=1;

				lcd_put_asciiR(500,120,'P');
				lcd_put_asciiR(510,120,'l');
				lcd_put_asciiR(520,120,'a');
				lcd_put_asciiR(530,120,'y');
				lcd_put_asciiR(540,120,'i');
				lcd_put_asciiR(550,120,'n');
				lcd_put_asciiR(560,120,'g');
				lcd_put_asciiR(570,120,'.');
				lcd_put_asciiR(580,120,'.');
				lcd_put_asciiR(590,120,'.');
			
				dlist_t p = find_by_index(headm, 1);
				
				//显示当前播放的音乐名称 
				int j = 0;
				while(j<20){
					lcd_put_ascii(500+j*10,150,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(500+j*10,150,p->name[j]);
					j++;
				}			
				
				//打开音乐文件 
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};

				//停止播放上一首 
				printf("\n停止播放\n");
				system("killall -KILL madplay &");
				sleep(1);

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
				printf("开始播放\n");
				flag = true;
				printf("%s\n",buf_p);
			
				//开始播放下一首 
				system(buf_p);

			}
			
			//点击文件，播放指定文件 
			if(xc>80&&xc<280&&yc>280&&yc<340){
				i = 2;
				lcd_put_asciiR(500,120,'P');
				lcd_put_asciiR(510,120,'l');
				lcd_put_asciiR(520,120,'a');
				lcd_put_asciiR(530,120,'y');
				lcd_put_asciiR(540,120,'i');
				lcd_put_asciiR(550,120,'n');
				lcd_put_asciiR(560,120,'g');
				lcd_put_asciiR(570,120,'.');
				lcd_put_asciiR(580,120,'.');
				lcd_put_asciiR(590,120,'.');
			
				dlist_t p = find_by_index(headm, 2);
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int j = 0;
				while(j<20){
					lcd_put_ascii(500+j*10,150,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(500+j*10,150,p->name[j]);
					j++;
				}
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};

				printf("\n停止播放\n");
				system("killall -KILL madplay &");
				sleep(1);

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
				printf("开始播放\n");
				flag = true;
				printf("%s\n",buf_p);
				sleep(1);
				system(buf_p);
			
			}
			
			//点击文件，播放指定文件 
			if(xc>80&&xc<280&&yc>340&&yc<430){
				i = 3;

				lcd_put_asciiR(500,120,'P');
				lcd_put_asciiR(510,120,'l');
				lcd_put_asciiR(520,120,'a');
				lcd_put_asciiR(530,120,'y');
				lcd_put_asciiR(540,120,'i');
				lcd_put_asciiR(550,120,'n');
				lcd_put_asciiR(560,120,'g');
				lcd_put_asciiR(570,120,'.');
				lcd_put_asciiR(580,120,'.');
				lcd_put_asciiR(590,120,'.');
			
				dlist_t p = find_by_index(headm, 3);
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int j = 0;
				while(j<20){
					lcd_put_ascii(500+j*10,150,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(500+j*10,150,p->name[j]);
					j++;
				}
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};
			
				printf("\n停止播放\n");
				system("killall -KILL madplay &");
				sleep(1);

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
				printf("开始播放\n");
				flag = true;
				
				printf("%s\n",buf_p);
				sleep(1);
				system(buf_p);
			
			}
			
			//暂停继续操作 
			if(xc>730&&xc<800&&yc>370&&yc<450){
				if(flag==true){
					printf("\n暂停播放\n");
					system("killall -STOP madplay &");
					flag = false;
					
				}else {
					printf("\n继续播放\n");
					system("killall -CONT madplay &");
					flag = true;
				}
				
			}
			
			//下一首 
			if(xc>850&&xc<940&&yc>370&&yc<450){
				i++;
				
				lcd_put_asciiR(500,120,'P');
				lcd_put_asciiR(510,120,'l');
				lcd_put_asciiR(520,120,'a');
				lcd_put_asciiR(530,120,'y');
				lcd_put_asciiR(540,120,'i');
				lcd_put_asciiR(550,120,'n');
				lcd_put_asciiR(560,120,'g');
				lcd_put_asciiR(570,120,'.');
				lcd_put_asciiR(580,120,'.');
				lcd_put_asciiR(590,120,'.');
			
				dlist_t p = find_by_index(headm, i);
				
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int j = 0;
				while(j<20){
					lcd_put_ascii(500+j*10,150,' ');
					j++;
				}				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(500+j*10,150,p->name[j]);
					j++;
				}
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};
				
				printf("\n停止播放\n");
				system("killall -KILL madplay &");
				sleep(1);

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
			
				printf("开始播放\n");
				flag = true;
				
				printf("%s\n",buf_p);
				
				system(buf_p);
			}			

			//上一首 
			if(xc>630&&xc<700&&yc>370&&yc<450){
				i--;
				
				lcd_put_asciiR(500,120,'P');
				lcd_put_asciiR(510,120,'l');
				lcd_put_asciiR(520,120,'a');
				lcd_put_asciiR(530,120,'y');
				lcd_put_asciiR(540,120,'i');
				lcd_put_asciiR(550,120,'n');
				lcd_put_asciiR(560,120,'g');
				lcd_put_asciiR(570,120,'.');
				lcd_put_asciiR(580,120,'.');
				lcd_put_asciiR(590,120,'.');
			
				dlist_t p = find_by_index(headm, i);
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int j = 0;
				while(j<20){
					lcd_put_ascii(500+j*10,150,' ');
					j++;
				}				
				
				sleep(1);
				j=0;
				while(p->name[j]!='\0'){
					lcd_put_asciiR(500+j*10,150,p->name[j]);
					j++;
				}
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};

				printf("\n停止播放\n");
				system("killall -KILL madplay &");
				sleep(1);

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
				printf("开始播放\n");
				flag = true;
				
				printf("%s\n",buf_p);
				
				system(buf_p);
			}	
		} 
	}
}

//缩小图片并显示操作 
//参数1：图片路径  参数2 ：映射关系 
void show_bmp1(const char *path,int *memp) 
{
	//图片参数 
	char buf[3] = {};
	int offset = 0,width = 0,height = 0;
	short bpp = 0;
	
	//1.打开图片文件 
	int fd = open(path,O_RDWR);
	if(fd ==-1){
		perror("open");
		return ;
	}
	
	//读文件类型
	read(fd,buf,2);
	//读图像数据里文件开头的距离
	lseek(fd,10,SEEK_SET);
	read(fd,&offset,4);
	//读图片宽度和高度
	lseek(fd,4,SEEK_CUR);
	read(fd,&width,4);
	read(fd,&height,4);
	//读像素位数
	lseek(fd,2,SEEK_CUR);
	read(fd,&bpp,2);
	
	//打印图片参数 
	printf("buf = %s\n",buf); 
	printf("offset = %d\n",offset);
	printf("width = %d,height = %d\n",width,height);
	printf("bpp = %hd\n",bpp);
	
	//读取RGB数据
	lseek(fd,offset,SEEK_SET);
	read(fd,picture,width*height*bpp/8);
	
	//显示
	//缩小一倍 
	int i = 0,j = 0;
	int i0 = 0,j0 = 0;
	for(i=0;i<240;i++){
		for(j=0;j<400;j++){
			i0 = i*960/480;
			j0 = j*1600/800;
			*(memp+i*800+j) = picture[3*((479-i0)*800+j0)]|  //B
			picture[3*((479-i0)*800+j0)+1]<<8| //G
			picture[3*((479-i0)*800+j0)+2]<<16;   //R
		}
		
	} 
	
	close(fd);

}

//图片操作
//参数1：映射关系   参数2 ：触摸屏 
void image_image(int *memp,int fdc)
{
	//显示初始化图片 
	show_bmp("/mnt/udisk/image_image.BMP",memp);
	
	int xc = 0,yc = 0;//触摸坐标

	bool flag = true;  //用于切换自动和手动操作  默认为手动操作 
	 
	int x_start = 0,y_start = 0;  //左划右划使用 
	
	int i = 0;  //用于锁定链表中文件位置

	struct dirent *dp = NULL;

	//创建成功 
	if(headi->next == headi->prev && headi->next == headi){
		
		//打开Image目录 
		DIR *dir = opendir("/mnt/udisk/Image");
		if(!dir){
			perror("opendir");
			return;
		}
		
		while(dp = readdir(dir)){
			//跳过文件名为  .  和  ..  的文件 
			if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
				continue;
			else {
				//将文件名插入到图片链表中 
				insert(headi->prev, dp->d_name);	
			}
		}
		
		
		//关闭Image目录 
		closedir(dir);
	}
	 
	char buf[256] = {};  //用于拼接文件路径 

	while(read(fdc,&evt,sizeof(evt))>0){
		if(evt.type==EV_ABS){
           	if(evt.code==ABS_X)
               	xc = evt.value;
           	else if(evt.code==ABS_Y)
              	yc = evt.value;
       	}
     	//按下时产生操作
       	else if(evt.type==EV_KEY&&evt.value==1){
			printf("(%d,%d)\n",xc,yc);
			
			//用于左划 右划 
			x_start = xc;
			y_start = yc;
			
			
		}
		
		//手动翻图片 
		if(evt.type == EV_KEY&&evt.value == 0){
			
			//下一张图片 
			if(xc>320&&xc<430&&yc>320&&yc<400){
				flag = false;
				i++;

				dlist_t p = find_by_index(headi, i);
				sprintf(buf,"/mnt/udisk/Image/%s",p->name);

				printf("%s\n",buf);
				show_bmp1(buf,memp);
				
				if(i == 4){
					show_bmp("/mnt/udisk/image_image.BMP",memp);	
				}
				
			}
			
			//上一张图片 
			if(xc>90&&xc<220&&yc>320&&yc<400){
				flag = false;
				i--;
				
				dlist_t p = find_by_index(headi, i);
				sprintf(buf,"/mnt/udisk/Image/%s",p->name);

				printf("%s\n",buf);
				show_bmp1(buf,memp);
				
				if(i == 0){
					show_bmp("/mnt/udisk/image_image.BMP",memp);
				}
			}
			
			//循环播放图片文件
			//播放一圈之后结束 
			if(xc>700&&xc<970&&yc>290&&yc<460){
				
				dlist_t p = find_by_index(headi, 1);
				while(p != headi){
					
					sprintf(buf,"/mnt/udisk/Image/%s",p->name);
					show_bmp1(buf,memp);
					sleep(1);
					p=p->next;
				}
				i = 0;
				show_bmp("/mnt/udisk/image_image.BMP",memp);
				flag = false;
			}
			
			if(xc>700&&xc<970&&yc>2&&yc<200){
				printf("手动操作 \n");
				flag = true;
			}

			//返回操作 
			if(xc>780&&xc<950&&yc>510&&yc<600){
				flag = false;
				return;
			}




			if(flag == true){

				if(xc>x_start){
					printf("右划!\n");
					i--;
					
					if(i == 0){
						i = 3;
					}
					
					dlist_t p = find_by_index(headi, i);
					sprintf(buf,"/mnt/udisk/Image/%s",p->name);

					printf("%s\n",buf);
					show_bmp1(buf,memp);
			
				}
				else if(xc<x_start){
					printf("左划!\n");
					i++;
					if(i == 4){
						i = 1;
					}

					dlist_t p = find_by_index(headi, i);
					sprintf(buf,"/mnt/udisk/Image/%s",p->name);

					printf("%s\n",buf);
					show_bmp1(buf,memp);
			
					
				}
			}
		}
	}
}

//文件操作
//参数 1 ：映射关系  参数2  ：触摸屏 
void file_image(int *memp,int fdc)
{
	
	//触摸位置   前一刻触摸位置  
	int xc = 0,yc = 0,x_prev = 0,y_prev = 0,x = 0,y = 0;
	
	char buf[999] = {};
	
	int res = 0;
	
	struct dirent *dp = NULL;

	int i = 0,j = 0;
	
	//将LCD显示屏分成3部分 
	for(i = 0;i<480;i++){
		for(j = 0;j<800/3;j++){
			*(memp + i*800 + j) = GROUP;
		}
		for(;j<800*2/3;j++){
			*(memp + i*800 + j) = A;
		}
		for(;j<800;j++){
			*(memp + i*800 + j) = BLUE|GREEN;
		}
	}

	//显示Preview 
	lcd_put_asciiR(230,900,'P');
	lcd_put_asciiR(240,900,'r');
	lcd_put_asciiR(250,900,'e');
	lcd_put_asciiR(260,900,'v');
	lcd_put_asciiR(270,900,'i');
	lcd_put_asciiR(280,900,'e');
	lcd_put_asciiR(290,900,'w');

	//显示Delete 
	lcd_put_asciiR(500,900,'D');
	lcd_put_asciiR(510,900,'e');
	lcd_put_asciiR(520,900,'l');
	lcd_put_asciiR(530,900,'e');
	lcd_put_asciiR(540,900,'t');
	lcd_put_asciiR(550,900,'e');

	//显示return 
	lcd_put_asciiR(680,900,'r');
	lcd_put_asciiR(690,900,'e');
	lcd_put_asciiR(700,900,'t');
	lcd_put_asciiR(710,900,'u');
	lcd_put_asciiR(720,900,'r');
	lcd_put_asciiR(730,900,'n');
	


	//显示Music 
	lcd_put_asciiR(100,70,'M');
	lcd_put_asciiR(110,70,'u');
	lcd_put_asciiR(120,70,'s');
	lcd_put_asciiR(130,70,'i');
	lcd_put_asciiR(140,70,'c');
	
	
	
	
	//创建成功操作 
	if(headm->next == headm->prev && headm->next == headm){
		
		//打开音乐目录 
		DIR *dir = opendir("/mnt/udisk/Music");
		if(!dir){
			perror("opendir");
			return;
		}
		
		while(dp = readdir(dir)){
			
			//跳过文件名为  .  和  ..  的文件 
			if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
				continue;
			else {
				//将文件名插入到音乐链表中 
				insert(headm->prev, dp->d_name);	
			}
		}
		
		
		//关闭Music目录 
		closedir(dir);
	}	

	//遍历音乐链表，即显示文件名称到LCD中 
	travelR(headm,memp);

	//显示Image 
	lcd_put_asciiR(360,70,'I');
	lcd_put_asciiR(370,70,'m');
	lcd_put_asciiR(380,70,'a');
	lcd_put_asciiR(390,70,'g');
	lcd_put_asciiR(400,70,'e');

	
	//创建成功 
	if(headi->next == headi->prev && headi->next == headi){
		
		//打开Image目录 
		DIR *dir = opendir("/mnt/udisk/Image");
		if(!dir){
			perror("opendir");
			return;
		}
		
		while(dp = readdir(dir)){
			//跳过文件名为  .  和  ..  的文件 
			if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
				continue;
			else {
				//将文件名插入到图片链表中 
				insert(headi->prev, dp->d_name);	
			}
		}
		
		
		
		//关闭Image目录 
		closedir(dir);
	}	

	//遍历Image链表，即显示文件名称到LCD中 	
	travelG(headi,memp);

	//显示Video 
	lcd_put_asciiR(620,70,'V');
	lcd_put_asciiR(630,70,'i');
	lcd_put_asciiR(640,70,'d');
	lcd_put_asciiR(650,70,'e');
	lcd_put_asciiR(660,70,'o');

	
	
	//创建成功 
	if(headv->next == headv->prev && headv->next == headv){
		
		//打开视频目录 
		DIR *dir = opendir("/mnt/udisk/Video");
		if(!dir){
			perror("opendir");
			return;
		}
		
		while(dp = readdir(dir)){
			
			////跳过文件名为  .  和  ..  的文件 
			if(strcmp(dp->d_name,".")==0||strcmp(dp->d_name,"..")==0)
				continue;
			else {
				//将文件名插入到视频链表中 
				insert(headv->prev, dp->d_name);
				
			}
		}
		
		
		
		//关闭Video目录 
		closedir(dir);
	}
	
	//遍历Video链表，即显示文件名称到LCD中 	
	travelB(headv,memp);

	while(read(fdc,&evt,sizeof(evt))>0){
		if(evt.type==EV_ABS){
           	if(evt.code==ABS_X)
               	xc = evt.value;
           	else if(evt.code==ABS_Y)
              	yc = evt.value;
       	}
     	//按下时产生操作
       	else if(evt.type==EV_KEY&&evt.value==1){
       		//记录前一个触摸坐标 
			x = x_prev;
			y = y_prev;
			x_prev = xc;
			y_prev = yc;
			printf("x_prev = %d\n",x);
			printf("y_prev = %d\n",y);
			printf("x = %d\n",xc);
			printf("y = %d\n",yc);
			
			//预览第一个音乐文件 
			if(x>5&&x<230&&	xc>240&&xc<400&& y>135&&y<200&& yc>451&&yc<598){
				dlist_t p = find_by_index(headm, 1);
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
				printf("开始播放\n");
				
				printf("%s\n",buf_p);
				system(buf_p);

				sleep(10);
				printf("\n停止播放\n");
				system("killall -KILL madplay &");
			}	
			
			//预览第二个音乐文件	
			if(x>5&&x<230&&	xc>240&&xc<400&&y>200&&y<280&&yc>451&&yc<598){
				dlist_t p = find_by_index(headm, 2);
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
				printf("开始播放\n");
				
				printf("%s\n",buf_p);
				system(buf_p);

				sleep(10);
				printf("\n停止播放\n");
				system("killall -KILL madplay &");
			}
			
			//预览第三个音乐文件 
			if(x>5&&x<230&&	xc>240&&xc<400&&y>280&&y<340&&yc>451&&yc<598){
				dlist_t p = find_by_index(headm, 3);
				sprintf(buf,"/mnt/udisk/Music/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open1");
					return ;
				}
				char buf_p[256]={};

				sprintf(buf_p,"madplay /mnt/udisk/Music/%s &",p->name);
				printf("开始播放\n");
				
				printf("%s\n",buf_p);
				system(buf_p);

				sleep(10);
				printf("\n停止播放\n");
				system("killall -KILL madplay &");
			}
				
			//删除第一个音乐文件		
			if(x>5&&x<230&& xc>600&&xc<740 &&y>135&&y<200&& yc>451&&yc<598){
				delete_by_index(headm,1);
				for(i = 0;i<480;i++){
					for(j = 0;j<800/3;j++){
						*(memp + i*800 + j) = GROUP;
					}
				}
				lcd_put_asciiR(100,70,'M');
				lcd_put_asciiR(110,70,'u');
				lcd_put_asciiR(120,70,'s');
				lcd_put_asciiR(130,70,'i');
				lcd_put_asciiR(140,70,'c');

				travelR(headm,memp);

				lcd_put_asciiR(230,900,'P');
				lcd_put_asciiR(240,900,'r');
				lcd_put_asciiR(250,900,'e');
				lcd_put_asciiR(260,900,'v');
				
			}
			
			//删除第二个音乐文件 
			if(x>5&&x<230&& xc>600&&xc<740 &&y>200&&y<280&& yc>451&&yc<598){
				delete_by_index(headm,2);
				for(i = 0;i<480;i++){
					for(j = 0;j<800/3;j++){
						*(memp + i*800 + j) = GROUP;
					}
				}
				lcd_put_asciiR(100,70,'M');
				lcd_put_asciiR(110,70,'u');
				lcd_put_asciiR(120,70,'s');
				lcd_put_asciiR(130,70,'i');
				lcd_put_asciiR(140,70,'c');

				travelR(headm,memp);

				lcd_put_asciiR(230,900,'P');
				lcd_put_asciiR(240,900,'r');
				lcd_put_asciiR(250,900,'e');
				lcd_put_asciiR(260,900,'v');
			}
			
			//删除第三个音乐文件 
			if(x>5&&x<230&&	xc>600&&xc<740 &&y>280&&y<340&& yc>451&&yc<598){
				delete_by_index(headm,3);
				for(i = 0;i<480;i++){
					for(j = 0;j<800/3;j++){
						*(memp + i*800 + j) = GROUP;
					}
				}
				lcd_put_asciiR(100,70,'M');
				lcd_put_asciiR(110,70,'u');
				lcd_put_asciiR(120,70,'s');
				lcd_put_asciiR(130,70,'i');
				lcd_put_asciiR(140,70,'c');

				travelR(headm,memp);

				lcd_put_asciiR(230,900,'P');
				lcd_put_asciiR(240,900,'r');
				lcd_put_asciiR(250,900,'e');
				lcd_put_asciiR(260,900,'v');
			}
			
			//预览第一个图片文件 
			if(x>360&&x<550&& xc>240&&xc<400&& y>135&&y<200&& yc>451&&yc<598){
				dlist_t p = find_by_index(headi, 1);

				sprintf(buf,"/mnt/udisk/Image/%s",p->name);

				
				printf("%s\n",buf);
				show_bmp(buf,memp);
				sleep(3);

				file_image(memp,fdc);
			}
			
			//预览第二个图片文件 
			if(x>360&&x<550&& xc>240&&xc<400&& y>200&&y<280&& yc>451&&yc<598){
				dlist_t p = find_by_index(headi, 2);

				sprintf(buf,"/mnt/udisk/Image/%s",p->name);

				
				printf("%s\n",buf);
				show_bmp(buf,memp);
				sleep(3);

				file_image(memp,fdc);
			}
			
			//预览第三个图片文件 
			if(x>360&&x<550&& xc>240&&xc<400&& y>280&&y<340&& yc>451&&yc<598){
				dlist_t p = find_by_index(headi, 3);

				sprintf(buf,"/mnt/udisk/Image/%s",p->name);

				
				printf("%s\n",buf);
				show_bmp(buf,memp);
				sleep(3);

				file_image(memp,fdc);
			}
			
			//删除第一个图片文件 
			if(x>360&&x<550&& xc>600&&xc<740 &&y>135&&y<200&& yc>451&&yc<598){
				delete_by_index(headi,1);
				for(i = 0;i<480;i++){
					for(j = 800/3;j<800*2/3;j++){
						*(memp + i*800 + j) = A;
					}
				}
				lcd_put_asciiR(360,70,'I');
				lcd_put_asciiR(370,70,'m');
				lcd_put_asciiR(380,70,'a');
				lcd_put_asciiR(390,70,'g');
				lcd_put_asciiR(400,70,'e');

				travelG(headi,memp);

				lcd_put_asciiR(230,900,'P');
				lcd_put_asciiR(240,900,'r');
				lcd_put_asciiR(250,900,'e');
				lcd_put_asciiR(260,900,'v');
				lcd_put_asciiR(270,900,'i');
				lcd_put_asciiR(280,900,'e');
				lcd_put_asciiR(290,900,'w');

				lcd_put_asciiR(500,900,'D');
				lcd_put_asciiR(510,900,'e');
				lcd_put_asciiR(520,900,'l');
				lcd_put_asciiR(530,900,'e');
				lcd_put_asciiR(540,900,'t');
				lcd_put_asciiR(550,900,'e');
				
			}
			
			//删除第二个图片文件 
			if(x>360&&x<550&& xc>600&&xc<740 &&y>200&&y<280&& yc>451&&yc<598){
				delete_by_index(headi,2);
				for(i = 0;i<480;i++){
					for(j = 800/3;j<800*2/3;j++){
						*(memp + i*800 + j) = A;
					}
				}
				lcd_put_asciiR(360,70,'I');
				lcd_put_asciiR(370,70,'m');
				lcd_put_asciiR(380,70,'a');
				lcd_put_asciiR(390,70,'g');
				lcd_put_asciiR(400,70,'e');

				travelG(headi,memp);

				lcd_put_asciiR(230,900,'P');
				lcd_put_asciiR(240,900,'r');
				lcd_put_asciiR(250,900,'e');
				lcd_put_asciiR(260,900,'v');
				lcd_put_asciiR(270,900,'i');
				lcd_put_asciiR(280,900,'e');
				lcd_put_asciiR(290,900,'w');

				lcd_put_asciiR(500,900,'D');
				lcd_put_asciiR(510,900,'e');
				lcd_put_asciiR(520,900,'l');
				lcd_put_asciiR(530,900,'e');
				lcd_put_asciiR(540,900,'t');
				lcd_put_asciiR(550,900,'e');
				
			}
			
			//删除第三个图片文件 
			if(x>360&&x<550&& xc>600&&xc<740 &&y>280&&y<340&& yc>451&&yc<598){
				delete_by_index(headi,3);
				for(i = 0;i<480;i++){
					for(j = 800/3;j<800*2/3;j++){
						*(memp + i*800 + j) = A;
					}
				}
				lcd_put_asciiR(360,70,'I');
				lcd_put_asciiR(370,70,'m');
				lcd_put_asciiR(380,70,'a');
				lcd_put_asciiR(390,70,'g');
				lcd_put_asciiR(400,70,'e');

				travelG(headi,memp);

				lcd_put_asciiR(230,900,'P');
				lcd_put_asciiR(240,900,'r');
				lcd_put_asciiR(250,900,'e');
				lcd_put_asciiR(260,900,'v');
				lcd_put_asciiR(270,900,'i');
				lcd_put_asciiR(280,900,'e');
				lcd_put_asciiR(290,900,'w');

				lcd_put_asciiR(500,900,'D');
				lcd_put_asciiR(510,900,'e');
				lcd_put_asciiR(520,900,'l');
				lcd_put_asciiR(530,900,'e');
				lcd_put_asciiR(540,900,'t');
				lcd_put_asciiR(550,900,'e');
				
			}
			
			//预览第一个视频文件 
			if(x>740&&x<970&& xc>240&&xc<400&& y>135&&y<200&& yc>451&&yc<598){
				dlist_t p = find_by_index(headv, 1);
				
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open");
					return ;
				}

				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 800 -y 480 /mnt/udisk/Video/%s &",p->name);
			
				system(buf_p);
				sleep(10);
				printf("\n停止播放\n");
				system("killall -KILL mplayer&");
				sleep(1);

				file_image(memp,fdc);
			}
			
			//预览第二个视频文件 
			if(x>740&&x<970&& xc>240&&xc<400&& y>200&&y<280&& yc>451&&yc<598){
				dlist_t p = find_by_index(headv, 2);
				
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open");
					return ;
				}

				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 800 -y 480 /mnt/udisk/Video/%s &",p->name);
			
				system(buf_p);
				sleep(10);
				printf("\n停止播放\n");
				system("killall -KILL mplayer&");
				sleep(1);
				file_image(memp,fdc);
			}
			
			//预览第三个视频文件 
			if(x>740&&x<970&& xc>240&&xc<400&& y>280&&y<340&& yc>451&&yc<598){
				dlist_t p = find_by_index(headv, 3);
				
				sprintf(buf,"/mnt/udisk/Video/%s",p->name);
				int fd = open(buf,O_RDONLY);
				if(fd == -1){
					perror("open");
					return ;
				}

				char buf_p[256]={};
				sprintf(buf_p,"mplayer -zoom -x 800 -y 480 /mnt/udisk/Video/%s &",p->name);
			
				system(buf_p);
				sleep(10);
				printf("\n停止播放\n");
				system("killall -KILL mplayer&");
				sleep(1);
				file_image(memp,fdc);
			}

			//删除第一个视频文件 
			if(x>740&&x<970&& xc>600&&xc<740 &&y>135&&y<200&& yc>451&&yc<598){
				delete_by_index(headv,1);
				for(i = 0;i<480;i++){
					for(j = 800*2/3;j<800;j++){
						*(memp + i*800 + j) = GREEN|BLUE;
					}
				}
				
				
				lcd_put_asciiR(620,70,'V');
				lcd_put_asciiR(630,70,'i');
				lcd_put_asciiR(640,70,'d');
				lcd_put_asciiR(650,70,'e');
				lcd_put_asciiR(660,70,'o');

				travelB(headv,memp);

				lcd_put_asciiR(680,900,'r');
				lcd_put_asciiR(690,900,'e');
				lcd_put_asciiR(700,900,'t');
				lcd_put_asciiR(710,900,'u');
				lcd_put_asciiR(720,900,'r');
				lcd_put_asciiR(730,900,'n');


				

				lcd_put_asciiR(500,900,'D');
				lcd_put_asciiR(510,900,'e');
				lcd_put_asciiR(520,900,'l');
				lcd_put_asciiR(530,900,'e');
				lcd_put_asciiR(540,900,'t');
				lcd_put_asciiR(550,900,'e');
				
			}
			
			//删除第二个视频文件 
			if(x>740&&x<970&& xc>600&&xc<740 &&y>200&&y<280&& yc>451&&yc<598){
				delete_by_index(headv,2);
				for(i = 0;i<480;i++){
					for(j = 800*2/3;j<800;j++){
						*(memp + i*800 + j) = GREEN|BLUE;
					}
				}
				
				
				lcd_put_asciiR(620,70,'V');
				lcd_put_asciiR(630,70,'i');
				lcd_put_asciiR(640,70,'d');
				lcd_put_asciiR(650,70,'e');
				lcd_put_asciiR(660,70,'o');

				travelB(headv,memp);

				lcd_put_asciiR(680,900,'r');
				lcd_put_asciiR(690,900,'e');
				lcd_put_asciiR(700,900,'t');
				lcd_put_asciiR(710,900,'u');
				lcd_put_asciiR(720,900,'r');
				lcd_put_asciiR(730,900,'n');


				

				lcd_put_asciiR(500,900,'D');
				lcd_put_asciiR(510,900,'e');
				lcd_put_asciiR(520,900,'l');
				lcd_put_asciiR(530,900,'e');
				lcd_put_asciiR(540,900,'t');
				lcd_put_asciiR(550,900,'e');
				
			}
			
			//删除第三个视频文件 
			if(x>740&&x<970&& xc>600&&xc<740 &&y>280&&y<340&& yc>451&&yc<598){
				delete_by_index(headv,3);
				for(i = 0;i<480;i++){
					for(j = 800*2/3;j<800;j++){
						*(memp + i*800 + j) = GREEN|BLUE;
					}
				}
				
				
				lcd_put_asciiR(620,70,'V');
				lcd_put_asciiR(630,70,'i');
				lcd_put_asciiR(640,70,'d');
				lcd_put_asciiR(650,70,'e');
				lcd_put_asciiR(660,70,'o');

				travelB(headv,memp);

				lcd_put_asciiR(680,900,'r');
				lcd_put_asciiR(690,900,'e');
				lcd_put_asciiR(700,900,'t');
				lcd_put_asciiR(710,900,'u');
				lcd_put_asciiR(720,900,'r');
				lcd_put_asciiR(730,900,'n');


				

				lcd_put_asciiR(500,900,'D');
				lcd_put_asciiR(510,900,'e');
				lcd_put_asciiR(520,900,'l');
				lcd_put_asciiR(530,900,'e');
				lcd_put_asciiR(540,900,'t');
				lcd_put_asciiR(550,900,'e');
				
			}
			
			//返回操作 
			if(xc>850&&xc<970&&yc>500&&yc<600)
				return;
		}
	}
}


//按位置删除，成功返回0，失败返回1
//参数1 ： 链表 参数2 ：位置 
int delete_by_index(dlist_t head, int index)
{
	//1.找到要删除的节点
	dlist_t del_node = find_by_index(head, index);
	if (!del_node) {
		return -1;
	}
	
	//2.删除该节点
	//将删除节点的前一个节点的后置指针改为指向删除节点的后一个节点
	del_node->prev->next = del_node->next;
	//将删除节点的后一个节点的前置指针改为指向删除节点的前一个节点
	del_node->next->prev = del_node->prev;
	//释放删除节点空间
	free(del_node);
	del_node = NULL;

	return 0;
}

//按位置查找，成功返回节点地址，失败返回NULL
//参数1 ： 链表 参数2 ：位置 
dlist_t find_by_index(dlist_t head, int index)
{
	int i = 1;
	dlist_t p = head->next;

	while (p != head) {
		if (i == index)
			return p;
		p = p->next;
		i++;
	}

	return NULL;
}

//创建空链表 ，成功返回创建的链表 
dlist_t create_emptydlist()
{
	//申请空间
	dlist_t head = (dlist_t)malloc(sizeof(dnode_t));
	if (head) {
		//head->name = -1;
		//空链表头节点的前置和后置指针都指向自己
		head->prev = head;
		head->next = head;
	}

	return head;
}

//往指定位置之后插入一个节点
//成功返回新插入节点的地址，失败返回空
//参数1 ：链表  参数2 ： 要插入的文件名称 
dlist_t insert(dlist_t p, char *file_name)
{
	//构造一个新节点
	dlist_t newnode = (dlist_t)malloc(sizeof(dnode_t));
	if (newnode) {
		int i = 0;
		while(file_name[i]!='\0'){
			newnode->name[i] = file_name[i];
			i++;
		}
		
		//新节点的前置指针应该指向插入的位置
		newnode->prev = p;
		//新节点的后置指针应该指向插入位置的下一个节点
		newnode->next = p->next;
		//插入位置的后置指针应指向新节点
		p->next = newnode;
		//插入位置的后一个节点的前置指针应指向新节点
		newnode->next->prev = newnode;

	}

	return newnode;
}

//遍历Music链表  在文件操作中使用 
//参数1 ： 链表   参数2 ：映射关系 
void travelR(dlist_t head,int *memp)
{
	dlist_t p = head->next;
	int i = 0,j = 0;
	//回到头节点遍历结束
	while (p != head) {
		
		while(i<strlen(p->name)){
			lcd_put_asciiR(20+i*10,120+j*60,p->name[i]);
			i++;
		}
		p = p->next;
		j++;
		i = 0;
		printf("\n");
	}
	
}


//遍历Music链表 在音乐操作中使用 
//参数1 ： 链表   参数2 ：映射关系 
void travelRR(dlist_t head,int *memp)
{
	dlist_t p = head->next;
	int i = 0,j = 0;
	//回到头节点遍历结束
	while (p != head) {
		
		while(i<strlen(p->name)){
			lcd_put_asciiR(80+i*10,180+j*60,p->name[i]);
			i++;
		}
		p = p->next;
		j++;
		i = 0;
		printf("\n");
	}
	
}

//遍历Image链表 
//参数1 ： Image链表  参数2 ：映射关系 
void travelG(dlist_t head,int *memp)
{
	dlist_t p = head->next;
	int i = 0,j = 0;
	//回到头节点遍历结束
	while (p != head) {
		
		while(i<strlen(p->name)){
			lcd_put_asciiR(300+i*10,120+j*60,p->name[i]);
			i++;
		}
		p = p->next;
		j++;
		i = 0;
		printf("\n");
	}
	
}

//遍历视频文件 文件操作中使用
//参数1 ： 视频链表  参数2 ：映射关系 
void travelB(dlist_t head,int *memp)
{
	dlist_t p = head->next;
	int i = 0,j = 0;
	//回到头节点遍历结束
	while (p != head) {
		while(p->name[i]!='\0'){
			lcd_put_asciiR(600+i*10,120+j*60,p->name[i]);
			i++;
		}
		p = p->next;
		j++;
		i = 0;
		printf("\n");
	}
	
}

//遍历Video链表  视频操作中使用
//参数1 ： Video 链表  参数2 ：映射关系 
void travelBB(dlist_t head,int *memp)
{
	dlist_t p = head->next;
	int i = 0,j = 0;
	//回到头节点遍历结束
	while (p != head) {
		while(p->name[i]!='\0'){
			lcd_put_asciiR(430+i*10,110+j*60,p->name[i]);
			i++;
		}
		p = p->next;
		j++;
		i = 0;
		printf("\n");
	}
	
}

//转换过渡 
void lcd_put_pixel(int x,int y,unsigned int color)
{
	unsigned char  *pen_8  = fbmem + y*line_width + x*pixel_width;
	unsigned short *pen_16;
	unsigned int   *pen_32;
 
	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;
 
	unsigned int red,green,blue;
 
	switch(var.bits_per_pixel){
		case 8 :{
			*pen_8 = color;
			break;
		}
 
		case 16 :{
			red   = (color >> 16) & 0xff;
			green = (color >> 8) & 0xff;
			blue  = (color >> 0) & 0xff;
 
			color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = color;
		
			break;
		}
 
		case 32 :{
			*pen_32 = color;
			break;
		}
 
		default:{
			printf("can't support %dbpp\n",var.bits_per_pixel);
			break;
		}
	}
}
 
//LCD上显示英文及符号 
void lcd_put_ascii(int x,int y,unsigned char c)
{
	unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
 
	int i,b;
	unsigned char byte;
 
	for(i=0;i<16;i++){
		byte = dots[i];
		for(b=7;b>=0;b--){
			if(byte & (1<<b)){
				/* show */
				lcd_put_pixel(x+7-b,y+i,RED1);
			}
			else{
				/* hide */
				lcd_put_pixel(x+7-b,y+i,RED|GREEN|BLUE);
			}
		}
	}
}
 
//LCD上显示英文及符号 
void lcd_put_asciiB(int x,int y,unsigned char c)
{
	unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
 
	int i,b;
	unsigned char byte;
 
	for(i=0;i<16;i++){
		byte = dots[i];
		for(b=7;b>=0;b--){
			if(byte & (1<<b)){
				/* show */
				lcd_put_pixel(x+7-b,y+i,RED1);
			}
			else{
				/* hide */
				lcd_put_pixel(x+7-b,y+i,0);
			}
		}
	}
}

 
//LCD上显示英文及符号 
void lcd_put_asciiR(int x,int y,unsigned char c)
{
	unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
 
	int i,b;
	unsigned char byte;
 
	for(i=0;i<16;i++){
		byte = dots[i];
		for(b=7;b>=0;b--){
			if(byte & (1<<b)){
				/* show */
				lcd_put_pixel(x+7-b,y+i,RED1);
			}
			//else{
				/* hide */
				//lcd_put_pixel(x+7-b,y+i,RED);
			//}
		}
	}
	
}


//LCD上显示中文 
void lcd_put_chinese(int x,int y,unsigned char *str)
{
	unsigned int area  = str[0] - 0xA1;
	unsigned int where = str[1] - 0xA1;
	unsigned char *dots = hzkmem + (area * 94 + where) * 32;
	unsigned char byte;
	int i = 0,j = 0,b = 0;
 
	for(i=0;i<16;i++){
		for(j=0;j<2;j++){
			byte = dots[i*2+j];
			for(b=7;b>=0;b--){
				if(byte & (1<<b)){
					/* show */
					lcd_put_pixel(x+j*8+7-b,y+i,0xffffff);
				}else{
					/* hide */
					lcd_put_pixel(x+j*8+7-b,y+i,0);
				}
			}
		}
	}
}
 

//用像素点画圆 
//参数1 映射文件 参数2和3： 横纵坐标 参数4：半径 
//返回值为空
void circle(int *memp,int x,int y,int r)
{
	int i = 0,j = 0;
	//缩小范围
		
	for(i = 0;i<360;i++){
		for(j = 0;j<800;j++){
			if(((i-x)*(i-x)+(y-j)*(y-j))<r*r){
				*(memp + i*800 + j)=RED|GREEN;
			}
			else *(memp + i*800 +j) = 0;

		}
	}	


}

//用像素点画板
void board(int *memp,int x)
{
	int i = 0,j = 0;
	
	for(i = 360;i<380;i++){
		for(j =0;j<800;j++){
			if(j>x-50&&j<x+50){
				*(memp + i*800 + j) = BLUE;
			}
			else *(memp + i*800 + j) = 0;
		}
	}	
} 

//板接球游戏
//参数1 ：映射关系 
//参数2 ： 触摸屏
//参数3 ： 点击事件 
void board_ball(int *memp,int fdc,struct input_event evt)
{
	show_bmp("/mnt/udisk/game.BMP",memp);
	
	
	int x = 0,y = 400,xc = 0,yc = 0;
	int x_prev = 0,y_prev = 0;
	int res = 0;
	int start = 1;
	
	circle(memp,x,y,10);
	
	while(read(fdc,&evt,sizeof(evt))>0){
		if(evt.type == EV_ABS){
			printf("1\n");
			if(evt.code == ABS_X){
				xc = evt.value;
			}
			else if(evt.code == ABS_Y){
				yc = evt.value;
			}
		} 
		
		if(evt.type == EV_KEY&&evt.value == 1){
			printf("(%d,%d)\n",xc,yc);
		}
		
		else if(evt.type == EV_KEY &&evt.value == 0){
			while(1){
				board(memp,500);
				x_prev = x-1;
				y_prev = y-1;
				
				circle(memp,x,y,10);
				x+=3;
				if(x==351)
				break;
			}
			while(1){/*
				while(read(fdc,&evt,sizeof(evt))>0){
					if(evt.type == EV_ABS){
						if(evt.code == ABS_X){
							xc = evt.value;
						}
					else if(evt.code == ABS_Y){
							yc = evt.value;
						}
					} 
		
					if(evt.type == EV_KEY&&evt.value == 1){
						printf("(%d,%d)\n",xc,yc);
						break;
					}
				}*/
				if(x == 351){
					printf("xc = %d\n",xc);
					//判断是否接住球 
					if((xc-100)>(y-50)&&(xc-100)<(y+50)){
						res += 1;
						printf("res = %d\n",res);	 			
					}
					
					//从左上来 , 到右上去 
					if(y_prev<y){
						while(1){
							x_prev = x;   //实时记录球的坐标（横） 
							y_prev = y;   //实时记录球的坐标（纵）
					
							circle(memp,x,y,10);  
							x--;
							board(memp,y);
							y++;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}		
						}
					}
					//从右上来 ，到左上去 
					else{
						while(1){
							x_prev = x;   //实时记录球的坐标（横） 
							y_prev = y;   //实时记录球的坐标（纵）
						
							circle(memp,x,y,10); 
							x--;
							board(memp,y);
							y--;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}
						}
					}	
				}
				//右边墙壁 
				if(y==800){
					//从左上来，到左下去 
					if(x_prev<x){
						while(1){
							x_prev=x;
							y_prev=y;
							circle(memp,x,y,10);
							x++;
							board(memp,y);
							y--;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}
						} 
					}
					//从左下来，到左上去 
					else{
						while(1){
							x_prev=x;
							y_prev=y;
							circle(memp,x,y,10);
							x--;
							board(memp,y);
							y--;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}
						} 
					}
				}
			
				//上边墙壁 
				if(x == 0){
					//从右下来，到左下去
					if(y_prev>y){
						while(1){
							x_prev=x;
							y_prev=y;
							circle(memp,x,y,10);
							x++;
							board(memp,y);
							y--;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}
						} 
					}
					//从左下来，到右下去 
					else{
						while(1){
							x_prev=x;
							y_prev=y;
							circle(memp,x,y,10);
							x++;
							board(memp,y);
							y++;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}
						}
					}
				}
				
				//左边墙壁 
				if(y == 0){
					//从右下来，到右上去 
					if(x_prev>x){
						while(1){
							x_prev=x;
							y_prev=y;
							circle(memp,x,y,10);
							x--;
							board(memp,y);
							y++;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}
						} 
					}
					//从右上来，到右下去 
					else{
						while(1){
							x_prev=x;
							y_prev=y;
							circle(memp,x,y,10);
							x++;
							board(memp,y);
							y++;
							if(x==351||x==0||y==800||y==0)
								{printf("x = %d,y = %d\n",x,y); break;}
						}
					}
				}	
			}
		}	
	}	
} 


